﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using hsgsSHXX.Web.Common;
namespace hsgsSHXX.Controllers
{
     [OutputCache(Duration = 0, VaryByParam = "none")]
    public class LoginController : Controller
    {
        //
        // GET: /Admin/Login/


        public ActionResult Index()
        {
            //这里判断是否登录
            if (Session["LoginID"] != null)
            {
                return RedirectToAction("Index", "Index"); ;
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult Login()
        {
            bool isSuccess = true;
            string errorMessage = "";
            string loginID = Fetch.Post("txtUserName").Trim();
            if (loginID == "")
            {
                errorMessage = "用户名不能为空！";
                isSuccess = false;
            }

            if (isSuccess)
            {
                string password = Fetch.Post("txtPassword").Trim();
                int result = SysLogin(loginID, password);
                if (result == 1)
                {
                    errorMessage = "用户名或密码错误请重新填写！";
                    isSuccess = false;
                }
                else if (result == 2)
                {
                    errorMessage = "你的帐号已经被禁止登陆！";
                    isSuccess = false;
                }
                else if (result == 3)
                {

                    errorMessage = "你的用户名已过期，不能登陆！";
                    isSuccess = false;
                }
                else if (result == 4)
                {
                    errorMessage = "ip受限制,请联系管理员!";
                }
                else if (result == 200)
                {
                    errorMessage = "";
                }
            }

            ViewData["username"] = loginID;
            if (errorMessage != "")
            {
                ViewData["jsAlert"] = errorMessage;
                return View("Index");
            }
            else
            {
                return RedirectToAction("Index", "Index");

            }

        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="loginID"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private int SysLogin(string loginID, string password)
        {
            try
            {
                //判断IP限制
                //string IP_Addr = Request.UserHostAddress;
                //string IP_Addrs = ConfigurationManager.AppSettings["IPAddr"];
                //string[] arrIP = IP_Addrs.Split('.');
                //for (int i = 0; i < arrIP.Count(); i++)
                //{
                //    if (arrIP[i].IndexOf("-") != -1)//设置了ip限制段
                //    {
                //        if (arrIP[i].IndexOf("|") != -1)//设置了多个ip段
                //        {
                //            string[] arrPart = arrIP[i].Split('|');
                //            for (byte k = 0; k < arrPart.Count(); k++)
                //            {
                //                byte a = Convert.ToByte(arrPart[k].Split('-')[0]);
                //                byte b = Convert.ToByte(arrPart[k].Split('-')[1]);
                //                byte t = Convert.ToByte(IP_Addr.Split('.')[i]);
                //                if (t >= a && t <= b)
                //                {
                //                    return 4;
                //                }
                //            }
                //        }
                //        else
                //        {
                //            byte a = Convert.ToByte(arrIP[i].Split('-')[0]);
                //            byte b = Convert.ToByte(arrIP[i].Split('-')[1]);
                //            byte t = Convert.ToByte(IP_Addr.Split('.')[i]);
                //            if (t >= a && t <= b)
                //            {
                //                return 4;
                //            }

                //        }
                //    }
                //    else
                //    {
                //        if (IP_Addr.Split('.')[i] != arrIP[i])
                //        {
                //            return 4;
                //        }
                //    }
                //}
                //if (IP_Addrs.IndexOf(IP_Addr) != -1)
                //{
                //    return 4;
                //}
            }
            finally
            {

            }

            BLL.Login adminLoginBLL = new BLL.Login();
            Model.PrvUser prvUserModel = adminLoginBLL.GetModelByNameAndPassword(loginID, password);
            if (prvUserModel == null) //用户名或密码错误
            {
                return 1;
            }
            else
            {
                
                OperateSession.add("LoginID", prvUserModel.FLogID);//把登陆ID保存到Session
                OperateSession.add("UserName", prvUserModel.FName);//登陆用户名称
                prvUserModel.FLast_Login_IP_Addr = Request.UserHostAddress;

                //OperateSession.add("LastLoginTime", string.Format("{0:yyyy-MM-dd}", prvUserModel.FLast_Login_Time));//上次登陆时间
                //OperateSession.add("LastLoginIPAddr", prvUserModel.FLast_Login_IP_Addr);//上次登陆IP
                //记录日志
                hsgsSHXX.BLL.OperateLog op = new hsgsSHXX.BLL.OperateLog();
                op.setOperateLog("", "", "", "登陆");
                
                return 200;
            }

        }

        /// <summary>
        /// Https用户验证
        /// </summary>
        /// <param name="loginID"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private int SysLoginByHttps(string loginID)
        {
            loginID = loginID.ToUpper();
            

            BLL.Login adminLoginBLL = new BLL.Login();
            Model.PrvUser prvUserModel = adminLoginBLL.GetModel(loginID);
            if (prvUserModel == null) //用户名或密码错误
            {
                return 1;
            }
            else
            {
                OperateSession.add("LoginID", prvUserModel.FLogID);//把登陆ID保存到Session
                OperateSession.add("UserName", prvUserModel.FName);//登陆用户名称
                prvUserModel.FLast_Login_IP_Addr = Request.UserHostAddress;
                //OperateSession.add("LastLoginTime", string.Format("{0:yyyy-MM-dd}", prvUserModel.FLast_Login_Time));//上次登陆时间
                //OperateSession.add("LastLoginIPAddr", prvUserModel.FLast_Login_IP_Addr);//上次登陆IP
                return 200;
            }

        }
    }
}
